/**
 * 全局变量
 */
var selectUserId = '';
$(function () {
    /*
     * 加载列表数据
     * */
    $('#listGrid').datagrid({
        url: ROOT_PATH + '/accidentEvent/accidentReport/queryListByParam.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        idField: 'id',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'id', width: 10, checkbox: true},
            {
                field: 'accidentName', title: '事故名称', width: 10, sortable: true,
                formatter: function (value, row, index) {
                    return "<a href='javascript:void(0);'style='color: deepskyblue;' >" + value + "</a>"
                }
            },
            {field: 'happenTime', title: '事故发生时间', width: 10, sortable: true},
            {field: 'accidentLevelName', title: '事故级别', width: 10},
            {field: 'accidentTypeName', title: '事故类别', width: 10, sortable: true},
            {field: 'directReason', title: '事故原因', width: 10, sortable: true},
            {field: 'educationSituation', title: '教育情况', width: 10, sortable: true},
            {field: 'ag', title: '事故责任者处理情况', width: 10, sortable: true},
            {field: 'ah', title: ' 验证状态', width: 10, sortable: true},
            /*{field:'appState',title:'状态',width:10,sortable:true,
             formatter:function(value,row,index){
             if(value===""||value===null||value===undefined){
             return "<span>未审批</span>";
             }else{
             if(value==-1){
             return "<span style='color: #FF0000;'>审批未通过</span>";
             }else if(value==1){
             return "<a href='javascript:void(0);' onclick=\"flowMonitorByBusinessId('"+row.id+"')\" style='color: #0abffc;'>审批通过</a>";
             }else{
             return "<a href='javascript:void(0);' onclick=\"flowMonitorByBusinessId('"+row.id+"')\" style='color: #FF8C00'>正在审批</a>"
             }
             }
             }
             },*/
            {field: 'addDeptName', title: '登记部门', width: 10, sortable: true},
            {field: 'addTime', title: '登记时间', width: 10, sortable: true}
        ]],
        toolbar: '#toolbar'

    });
    /**
     * 加载下拉列表信息
     *
     */
    queryDepartmentTree();//加载部门选项
    //加载数据字典下拉列表
    queryDictionaryList("searchAccidentLevel", CONSTANT.DictionaryTypeDefConstants.ACCIDENT_LEVEL);//事故等级
    queryDictionaryList("searchAccidentType", CONSTANT.DictionaryTypeDefConstants.ACCIDENT_TYPE);//事故类别
    /**
     * 绑定人员选择事件
     */
    $("#searchChargeUser").textbox({
        iconWidth: 22,
        icons: [{
            iconCls: 'icon-add',
            handler: function (e) {
                selectUserId = "searchChargeUser";
                add_contractorInfo_func();
            }
        },
            {
                iconCls: 'icon-remove',
                handler: function (e) {
                    $(e.data.target).textbox('clear');

                }
            }
        ]
    });
    $("#searchAddUser").textbox({
        iconWidth: 22,
        icons: [
            {
                iconCls: 'icon-add',
                handler: function (e) {
                    selectUserId = "searchAddUser";
                    add_contractorInfo_func();
                }
            },
            {
                iconCls: 'icon-remove',
                handler: function (e) {
                    $(e.data.target).textbox('clear');

                }
            }
        ]
    });
    //绑定按钮事件
    $('#btn_advanced_search').on("click", function () {
        $('#searchForm2').toggle();
        $('#hdList').datagrid('resize');
    });

    $("#btn_ae_accidentEvent_accidentEvaluation_edit").on("click", function () {
        evaluation();
    });
    /***
     * 提交审批
     * @returns {boolean}
     */
    $('#btn_hd_hdRegister_submit').on('click', function () {
        submitHDRegister();
    });
    //查询按钮绑定事件
    $("#btn_search").on('click', function () {
        searchFun();
    });
    $('#btn_clear').on('click', function () {
        $('#searchForm1').form('clear');
        $('#searchForm2').form('clear');
    });

});
/**
 * 打开添加页面
 */
function addAccidentEvaluation() {
    WindowManager.openLayer({
        type: 2,
        title: '新增事故后评估信息',
        area: ['1200px', '90%'],
        content: ROOT_PATH + '/accidentEvent/accidentEvaluation/add.shtml',
        callBack: function (data) {
            $('#listGrid').datagrid("reload");
        }
    });
}
/**
 * 打开编辑页面
 */
function evaluation() {
    var selectedRow = $('#listGrid').datagrid('getSelected');
    if (selectedRow) {
        var accidentReportId = selectedRow.id;
        WindowManager.openLayer({
            type: 2,
            title: '事故信息评估',
            area: ['1200px', '90%'],
            content: ROOT_PATH + '/accidentEvent/accidentEvaluation/evaluation.shtml?accidentReportId=' + accidentReportId,
            callBack: function (data) {
                $('#listGrid').datagrid("reload");
            }
        });
    } else {
        layer.alert("请选择数据！");
    }

}
/**
 * 打开详情页面
 */
function openDetailPage(businessId) {
    WindowManager.openLayer({
        type: 2,
        title: '事故后评估信息',
        area: ['1200px', '90%'],
        content: ROOT_PATH + '/hiddenDanger/hdRegister/detail.shtml?businessId=' + businessId
    });
    /* var index = top.layer.open({
     type: 2,
     title: '隐患信息登记',
     area: ['1200px', '90%'],
     content: ROOT_PATH + '/hiddenDanger/hdRegister/detail.shtml?businessId='+businessId,
     end:function(){
     $('#hdList').datagrid("reload");
     }
     });*/

}
/**
 * 删除方法
 */
function deleteHDRegister() {
    var selectedRow = $('#hdList').datagrid('getSelected');
    if (selectedRow) {
        var businessId = selectedRow.id;
        layer.confirm("确定删除该数据？", {btn: ['确定', '取消']}, function () {
            $.ajax({
                url: ROOT_PATH + '/hiddenDanger/hdRegister/delete.shtml',//提交地址
                data: {businessId: businessId},//将表单数据序列化
                type: "POST",
                dataType: "json",
                success: function (result) {
                    //console.log(result);
                    if (result.success) {
                        layer.msg(result.message, {time: 1000, icon: 1}, function () {
                            $('#hdList').datagrid('reload');
                        });
                    } else {
                        layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        });
                    }
                }
            });
        });

        /*var index = top.layer.open({
         type: 2,
         title: '隐患信息登记',
         area: ['1200px', '90%'],
         content: ROOT_PATH + '/hiddenDanger/hdRegister/edit.shtml?businessId='+businessId,
         end:function(){
         $('#hdList').datagrid("reload");
         }
         });*/
    } else {
        layer.alert("请选择数据！");
    }

}
/**
 * 提交审批的方法
 * @returns {boolean}
 */
function submitHDRegister() {
    var $selected = $('#hdList').datagrid('getSelections');
    if ($selected.length != 1) {
        layer.alert('请选择一条数据！');
        return false;
    }
    var businessId = $selected[0].id;//请假单id
    console.log();
    if (!($selected[0].appState == null || $selected[0].appState == "")) {
        layer.alert('业务流程已存在，禁止重复提交！');
        return false;
    }
    var layerIndex = layer.confirm('是否确定提交审批？', function () {
        layer.close(layerIndex);
        /***
         * 选择处理人
         */
        WindowManager.openLayer({
            type: 2,
            title: '选择下一环节处理人',
            area: ['370px', '180px'],
            content: ROOT_PATH + '/workflow/flowManagement/toFlowNextUserSelectPage.shtml',
            data: {id: 'ff', name: '我是名称'},
            callBack: function (data) {//窗口关闭后的回调函数
                console.log(data);
                //得到选择的人员
                if (data == null || data === "") {
                    layer.alert("请选择处理人!");
                    return false;
                }
                $.ajax({
                    type: "POST",
                    url: ROOT_PATH + '/hiddenDanger/hdRegister/submitHDRegisterApplication.shtml',
                    data: {id: $selected[0].id, nextUserId: data},
                    success: function (result) {
                        result = $.parseJSON(result);
                        if (result.success) {
                            layer.msg(result.message, {time: 1000, icon: 1}, function () {
                                $('#hdList').datagrid('load');
                                layer.closeAll(); //先得到当前iframe层的索引
                            });
                        } else {
                            layer.msg(result.message);
                        }
                    }
                });
            }
        });

    });
}
/**
 * 查询按钮方法
 */
function searchFun() {
    var search1 = $('#searchForm1').serializeJson();
    var search2 = $('#searchForm2').serializeJson();
    var search = $.extend(search1, search2);
    //排序参数
    search.sort = "addTime";
    search.order = 'desc';
    $('#listGrid').datagrid('load', search);
}

/**
 * 查询部门树方法
 */
function queryDepartmentTree() {
    $.ajax({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data: {parentId: 0},//将表单数据序列化
        type: "POST",
        dataType: "json",
        success: function (result) {
            //隐患所在部门
            $('#searchDept').combotree(
                {
                    data: result,
                    loadFilter: function (data) {
                        var newData = new Array();
                        newData.push({id: "", text: '请选择'});
                        newData.push(data[0]);
                        return newData;
                    }
                }
            );
            //隐患登记部门
            $('#searchAddDept').combotree(
                {
                    data: result,
                    loadFilter: function (data) {
                        var newData = new Array();
                        newData.push({id: "", text: '请选择'});
                        newData.push(data[0]);
                        return newData;
                    }
                }
            );

        }
    });
}
/**
 * 人员信息选择
 */
/*var getSelectedUser = function (data) {
 // console.log(data);
 //给行编辑赋值
 if(selectUserId==""||selectUserId==null){
 var dutyUserED= datagrid.datagrid('getEditor', {index:editRow,field:'dutyUserName'});
 var dutyUserIdED= datagrid.datagrid('getEditor', {index:editRow,field:'dutyUserId'});
 $(dutyUserED.target).textbox("setValue",data[0].name)
 $(dutyUserIdED.target).textbox("setValue",data[0].id)
 }else{//给正常文本框赋值
 $("#"+selectUserId).textbox('setValue',data[0].id);
 $("#"+selectUserId).textbox('setText',data[0].name);
 selectUserId = "";
 }

 //console.log();


 //$("#"+selectUserId+"Id").val(data.id);
 // $("#"+selectUserId).textbox('setValue','值123');
 //$("#"+selectUserId).val(data.id);
 // $("#"+selectUserId).text(data.name);
 return data;
 };*/
var add_contractorInfo_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack: function (data) {//窗口关闭后的回调函数
            if (selectUserId == "" || selectUserId == null) {
                var dutyUserED = datagrid.datagrid('getEditor', {index: editRow, field: 'dutyUserName'});
                var dutyUserIdED = datagrid.datagrid('getEditor', {index: editRow, field: 'dutyUserId'});
                $(dutyUserED.target).textbox("setValue", data[0].name)
                $(dutyUserIdED.target).textbox("setValue", data[0].id)
            } else {//给正常文本框赋值
                $("#" + selectUserId).textbox('setValue', data[0].id);
                $("#" + selectUserId).textbox('setText', data[0].name);
                selectUserId = "";
            }
        }
    });
    /*layer.open({
     type: 2,
     title: '选择人员信息',
     area: ['1150px', '70%'],
     content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0'
     });*/
};

/**
 * 查询数据字典列表的方法
 * @param id
 * @param code
 */
function queryDictionaryList(id, code) {
    $.ajax({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',//提交地址
        data: {code: code},//将表单数据序列化
        type: "POST",
        dataType: "json",
        success: function (result) {
            $('#' + id).combobox(
                {
                    data: result,
                    valueField: 'id',
                    textField: 'text',
                    loadFilter: function (data) {
                        var newData = new Array();
                        newData.push({id: "", text: '请选择'});
                        for (var i = 0; i < data.length; i++) {
                            newData.push(data[i])
                        }
                        return newData;
                    }
                }
            );

        }
    });
}
/**
 * 设置年度下拉列表
 * @param id
 * @param code
 */
function queryYearthList(id) {
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    yearthData.push({id: "", text: "请选择"});
    for (var i = 0; i < 6; i++) {
        var y = {id: year - i, text: (year - i) + '年'}
        yearthData.push(y);
    }
    $('#' + id).combobox(
        {
            data: yearthData,
            valueField: 'id',
            textField: 'text'
        }
    );
}
/***
 * 根据业务id查询流程信息
 * @param row
 */
function flowMonitorByBusinessId(businessId) {
    var index = layer.open({
        type: 2,
        title: '业务流程监控',
        area: ['80%', '80%'],
        content: ROOT_PATH + '/workflow/flowManagement/flowMonitorByBusinessId.shtml?businessId=' + businessId
    });
}